#!/usr/bin/bash
# Copyright (c) 2023. huawei Technologies Co.,Ltd.ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
#          http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author    :   zhuwenshuo
# @Contact   :   1003254035@qq.com
# @Date      :   2023/02/21
# @License   :   Mulan PSL v2
# @Desc      :   Test zentest
# #############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function pre_test() {
    LOG_INFO "Start to prepare the test environment."
    DNF_INSTALL "rubygem-ZenTest tar rubygem-minitest rubygem-test-unit nodejs ruby-devel rubygem-rails bc"
    yum groupinstall "Development Tools" -y
    tar -xvf ./common/data.tar.gz > /dev/null
    LOG_INFO "End to prepare the test environment."
}
function run_test() {
    LOG_INFO "Start to run test."
    zentest -h | grep "ZenTest scans your target"
    CHECK_RESULT $? 0 0 "Check zentest -h failed"
    zentest -v | grep "$(rpm -qa rubygem-ZenTest | awk -F '-' '{print $3}')"
    CHECK_RESULT $? 0 0 "Check zentest -v failed"
    zentest ./data/example1.rb  ./data/example2.rb | grep "Code Generated by ZenTest"
    CHECK_RESULT $? 0 0 "Check zentest basic failed"
    zentest -e ./data/example1.rb | grep "NotImplementedError"
    CHECK_RESULT $? 0 0 "Check zentest -e failed"
    zentest -t ./data/example1.rb | grep "class TestThingy < Test::Unit::TestCase"
    CHECK_RESULT $? 0 0 "Check zentest -t failed"
    autotest -h | grep "Continuous testing for your ruby app"
    CHECK_RESULT $? 0 0 "Check autotest -h failed"
    version=$(ruby --version | awk '{print $2}' | cut -c1-3)
    if [[ $(echo "${version} >= 2.6"|bc) -eq 1 ]] && [[ $(echo "${version} < 4.0"|bc) -eq 1 ]]; then
        rails new demo
        pushd demo
        zentest -r ./db/seeds.rb | grep "Number of errors"
        CHECK_RESULT $? 0 0 "Check zentest -r failed"
        popd
    fi
    LOG_INFO "End to run test."
}
function post_test() {
    LOG_INFO "Start to restore the test environment."
    DNF_REMOVE
    rm -rf lib/ data/ demo
    LOG_INFO "End to restore the test environment."
}
main "$@"
